package com.wangjw.coreTechnology;

import java.util.concurrent.locks.StampedLock;

/**
 * Created by wangjw on 18/7/19.
 */
public class StampedSample {

    private final StampedLock stampedLock = new StampedLock();

    void mutate() {
        long stamp = stampedLock.writeLock();
        try {
            write();
        } finally {
            stampedLock.unlockWrite(stamp);
        }
    }

    private void write() {

    }

    Object access() {
        long stamp = stampedLock.tryOptimisticRead();
        Object object = read();
        if (!stampedLock.validate(stamp)) {
            stamp = stampedLock.readLock();
            try {
                object = read();
            } finally {
                stampedLock.unlockRead(stamp);
            }
        }

        return object;
    }

    private Object read() {
        return null;
    }
}
